<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="__STATIC__/lay/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="__STATIC__/lay/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>

<body>
    <div class="layui-form layuimini-form" lay-filter="formTest1">
        <div class="layui-form-item">
            <label class="layui-form-label required">管理员账号</label>
            <div class="layui-input-block">
                <input type="text" name="admin" lay-verify="required" lay-reqtext="管理员账号不能为空" placeholder="请输入账号"
                    value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">管理员密码</label>
            <div class="layui-input-block">
                <input type="text" name="pwd1" lay-verify="required" lay-reqtext="管理员密码不能为空" placeholder="请输入密码"
                    value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">确认密码</label>
            <div class="layui-input-block">
                <input type="text" name="pwd2" lay-verify="required" lay-reqtext="确认密码不能为空" placeholder="请输入确认密码"
                    value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">身份</label>
            <div class="layui-input-block">
                <select name="role" id="roleselect">


                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input type="text" name="sort" placeholder="数值大的靠前" value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
    <script src="__STATIC__/lay/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
    <script src="__STATIC__/lay/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
    <script>
        layui.use(['form'], function () {
            var form = layui.form,
                layer = layui.layer,
                $ = layui.$;
            var adminid = parent.layer.methodConfig.data.id
            console.log(adminid)
            //请求列表
            $.ajax({
                type: "post",
                url: "/admin/admin/init",
                data: {},
                dataType: "json",
                success: function (res) {
                    // var str = ""
                    $.each(res.data, function (key, value) {
                        $('#roleselect').append(new Option(value.name, value.id))
                    });

                    if (adminid != 0) {
                        $.ajax({
                            type: "post",
                            url: "/admin/admin/find",
                            data: {
                                id: adminid
                            },
                            dataType: "json",
                            success: function (res) {
                                form.val('formTest1', {
                                    "admin": res.data.admin,
                                    "role": res.data.role,
                                    "sort": res.data.sort

                                })
                            }
                        });
                    }


                    form.render();
                }
            });



            //监听提交
            form.on('submit(saveBtn)', function (data) {

                if (adminid != 0) {
                    data.field.id = adminid
                    $.ajax({
                        type: "post",
                        url: "/admin/admin/editval",
                        data: data.field,
                        dataType: "json",
                        success: function (response) {
                            if (response.code == 1) {
                                // layer.close(index);
                                parent.window.location.reload()
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);

                            } else {
                                layer.msg(response.msg)
                            }
                        }
                    });
                } else {
                    $.ajax({
                        type: "post",
                        url: "/admin/admin/addvalue",
                        data: data.field,
                        dataType: "json",
                        success: function (response) {
                            if (response.code == 1) {
                                // layer.close(index);
                                parent.window.location.reload()
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);

                            } else {
                                layer.msg(response.msg)
                            }
                        }
                    });
                }



                return false;
            });

        });
    </script>
</body>

</html>